package com.fbt.energy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbt.energy.dao.SpProductionLineDao;
import com.fbt.energy.entity.AO.SpAO;
import com.fbt.energy.entity.AO.SpEqAO;
import com.fbt.energy.entity.AO.SpPrAO;
import com.fbt.energy.entity.SpEquipment;
import com.fbt.energy.entity.SpProductionLine;
import com.fbt.energy.entity.SparePart;
import com.fbt.energy.service.SpEquipmentService;
import com.fbt.energy.service.SpProductionLineService;
import com.fbt.energy.service.SparePartService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 备件_产线表(SpProductionLine)表服务实现类
 *
 * @author makejava
 * @since 2022-01-21 11:56:11
 */
@Service("spProductionLineService")
public class SpProductionLineServiceImpl extends ServiceImpl<SpProductionLineDao, SpProductionLine> implements SpProductionLineService {
    @Resource
    private SpProductionLineDao spProductionLineDao;

    @Resource
    private SpProductionLineService spPrService;

    @Resource
    private SpEquipmentService spEqService;

    @Resource
    private SparePartService sparePartService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SpProductionLine queryById(Integer id) {
        return this.spProductionLineDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param spProductionLine 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<SpProductionLine> queryByPage(SpProductionLine spProductionLine, PageRequest pageRequest) {
        long total = this.spProductionLineDao.count(spProductionLine);
        return new PageImpl<>(this.spProductionLineDao.queryAllByLimit(spProductionLine, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param spProductionLine 实例对象
     * @return 实例对象
     */
    @Override
    public SpProductionLine insert(SpProductionLine spProductionLine) {
        this.spProductionLineDao.insert(spProductionLine);
        return spProductionLine;
    }

    /**
     * 修改数据
     *
     * @param spProductionLine 实例对象
     * @return 实例对象
     */
    @Override
    public SpProductionLine update(SpProductionLine spProductionLine) {
        this.spProductionLineDao.update(spProductionLine);
        return this.getSpPrAOByPrNo(spProductionLine.getPrNo());
    }

    /**
     * 通过主键删除数据
     */
    @Override
    public boolean deleteById(String prNo) {
        return this.spProductionLineDao.deleteById(prNo) > 0;
    }

    /**根据产线编号查所有*/
    @Override
    public SpPrAO getSparePartByPrNo(String prNo) {

        QueryWrapper<SpProductionLine> spPrWrapper = new QueryWrapper<>();
//产线
        System.out.println("impi    " + prNo);
        spPrWrapper.eq("pr_no    ", prNo);
        SpProductionLine spPrServiceOne = spPrService.getOne(spPrWrapper);
        String plName = spPrServiceOne.getPrName();
        SpPrAO spPrAO = new SpPrAO();
        spPrAO.setPlName(plName);
        spPrAO.setPlNo(prNo);


        List<SpEquipment> eqList = spEqService.getSpEquipments(prNo);
        List<SpEqAO> spEqAOList = new ArrayList<>();


        for (SpEquipment spEq : eqList) {
            SpEqAO spEqAO = new SpEqAO();
            String eqNo = spEq.getEqNo();
            String eqName = spEq.getEqName();

            spEqAO.setEqName(eqName);
            spEqAO.setEqNo(eqNo);


            spEqAOList.add(spEqAO);
//备件
            List<SparePart> spareParts = sparePartService.getsparePars(eqNo);
            List<SpAO> spAOList = new ArrayList<>();

            for (SparePart sp : spareParts) {
                String spNo = sp.getSpNo();
                String sparePartName = sp.getSparePartName();

                String ms = sp.getMs();

                String hiddenDanger = sp.getHiddenDanger();

                SpAO spAO = new SpAO();

                spAO.setSpNo(spNo);
                spAO.setSpName(sparePartName);
                spAO.setMs(ms);
                List<String> pictureList = sparePartService.getPictureBySpNo(spNo);
                spAO.setPictureList(pictureList);
                spAO.setHiddenDanger(hiddenDanger);
                spAOList.add(spAO);
            }
            spEqAO.setSpAOList(spAOList);
        }
        spPrAO.setSpEqAOList(spEqAOList);
        return spPrAO;
    }


    /**
     * 查询所有产线
     */
    @Override
    public List<SpPrAO> getAllSparePart() {
        List<SpProductionLine> spPrs = spPrService.list(null);

        List<SpPrAO> spPrList = new ArrayList<>();

        for (SpProductionLine spPr : spPrs) {
            SpPrAO spPrAO = new SpPrAO();
            String prNo = spPr.getPrNo();
            String prName = spPr.getPrName();

            spPrAO.setPlName(prName);
            spPrAO.setPlNo(prNo);


            spPrList.add(spPrAO);
        }
        return spPrList;
    }

    /**
     * 根据产线编号查产线
     */
    @Override
    public SpProductionLine getSpPrAOByPrNo(String prNo) {

        QueryWrapper<SpProductionLine> spProductionLineQueryWrapper = new QueryWrapper<>();
        spProductionLineQueryWrapper.eq("pr_no", prNo);
        SpProductionLine spPrServiceOne = spPrService.getOne(spProductionLineQueryWrapper);
        return spPrServiceOne;
    }


}
